package net.zjitc.controller;

import net.zjitc.pojo.Category;
import net.zjitc.pojo.Goods;
import net.zjitc.service.CategoryService;
import net.zjitc.service.GoodsService;
import net.zjitc.utils.APIResponse;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/findAll")
    @CrossOrigin(origins = "*")
    public APIResponse<Map<String, Object>> getGoodsList(@RequestParam int page, @RequestParam int pagesize){
        Map<String, Object> result = goodsService.findAllWithPagination(page, pagesize);
        APIResponse<Map<String, Object>> api = new APIResponse<>();
        api.ok("查询成功", result);
        return api;
    }


//@PostMapping("/delete")
//@CrossOrigin(origins = "*")
//public APIResponse<String> delete(@RequestBody Map<String, Integer> request) {
//    // 从请求体中提取id
//    Integer id = request.get("id");
//    vueShopCategory.delete(id);
//    APIResponse<String> api = new APIResponse<>();
//    api.ok("删除成功", "");
//    return api;
//}
@PostMapping("/delete")
@CrossOrigin(origins = "*")
public APIResponse<String> delete(@RequestBody Map<String, Integer> request) {
    // 从请求体中提取id
    Integer id = request.get("id");
    goodsService.delete(id);
    APIResponse<String> api = new APIResponse<>();
    api.ok("删除成功", "");
    return api;
}

//    @PostMapping("/add")
//    @CrossOrigin(origins = "*")
//    public APIResponse<String> insertShop(@RequestBody Category category) {
//        APIResponse<String> api = new APIResponse<>();
//        if(category.getPid()==null){
//            category.setPid(0);
//        }
//        vueShopCategory.insertShop(category);
//
//        api.ok("添加成功", "");
//        return api;
//    }
    @PostMapping("/add")
    @CrossOrigin(origins = "*")
    public APIResponse<String> insertShop(@RequestBody Goods goods) {
        APIResponse<String> api = new APIResponse<>();
//        goods.setCategoryId(7);
        goodsService.insertShop(goods);
        api.ok("添加成功", "");
        return api;
    }
//    @GetMapping("/findById")
//    @CrossOrigin(origins = "*")
//    public APIResponse<Category> findById(@RequestParam Integer id){
//        Category shop = vueShopCategory.findById(id);
//        APIResponse<Category> api = new APIResponse<>();
//        api.ok("查询单个成功", shop);
//        return api;
//    }
@GetMapping("/findById")
@CrossOrigin(origins = "*")
public APIResponse<Goods> findById(@RequestParam Integer id){
    Goods shop = goodsService.findById(id);
    APIResponse<Goods> api = new APIResponse<>();
    api.ok("查询单个成功", shop);
    return api;
}

//    @PostMapping("update")
//    @CrossOrigin(origins = "*")
//    public APIResponse<String> update(@RequestBody Category category){
//        vueShopCategory.update(category);
//        APIResponse<String> api = new APIResponse<>();
//        api.ok("更新成功", "");
//        return api;
//    }
    @PostMapping("update")
    @CrossOrigin(origins = "*")
    public APIResponse<String> update(@RequestBody Goods goods){
        goodsService.update(goods);
        APIResponse<String> api = new APIResponse<>();
        api.ok("更新成功", "");
        return api;
    }
}
